package com.ggh.jinjilive.ui.live;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.create.StartLiveMobile;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.ggh.jinjilive.mobile.live.LiveMessageMobile;
import com.ggh.jinjilive.mobile.live.SellGoodsMobile;
import com.ggh.jinjilive.mobile.live.im.CustomMessage;
import com.ggh.jinjilive.mobile.login.LoginMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;
import com.ggh.jinjilive.views.live.pk.commondef.AnchorInfo;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TVLiveViewModel extends BaseViewModel {
    //房间信息
    public ObservableField<StartLiveMobile> startLiveMobileObservableField = new ObservableField<>();
    //直播人数
    public ObservableField<Integer> number = new ObservableField<>(0);
    //直播间信息
    public MutableLiveData<LiveMessageMobile> liveMessageMobileMutableLiveData = new MutableLiveData<>();
    //直播间列表
    public MutableLiveData<List<LiveList>> liveData = new MutableLiveData<>();
    //直播列表
    public MutableLiveData<List<StartLiveMobile>> StartLiveMobileList = new MutableLiveData<>();
    //区分是邀请还是收到
    public ObservableField<Boolean> differentiate = new ObservableField<>();

    //任务列表
    public MutableLiveData<List<SellGoodsMobile>> mSellGoodsMobileList = new MutableLiveData<>();

    public TVLiveViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 更新直播间人数
     */
    public void realLiveUpdate(){
        Map<String, Object> map = new HashMap<>();
        map.put("live_id",startLiveMobileObservableField.get().getLive_id());
        map.put("number",number.get());
        addSubscribe(apiCallInterface.realLiveUpdate(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }
        }));
    }



    /**
     * 直播间信息
     */
    public void anchorView(String user_id){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("user_id",user_id);
        paramsa.put("anchor_id",user_id);
        addSubscribe(apiCallInterface.anchorView(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<LiveMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                LiveMessageMobile liveMessageMobile = (LiveMessageMobile) resultEntity.getData();
                liveMessageMobileMutableLiveData.setValue(liveMessageMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 直播间信息
     */
    public void livePk(String on_user_id){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("user_id",MerchantSession.getInstance().getUser().getUser_id());
        paramsa.put("on_user_id",on_user_id);
        addSubscribe(apiCallInterface.livePk(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<StartLiveMobile>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<StartLiveMobile> startLiveMobileList = (List<StartLiveMobile>) resultEntity.getData();
                StartLiveMobileList.setValue(startLiveMobileList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 领取直播间红包雨
     */
    public void getRedsrain(){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("user_id",MerchantSession.getInstance().getUser().getUser_id());
        paramsa.put("live_id",startLiveMobileObservableField.get().getLive_id());
        addSubscribe(apiCallInterface.getRedsrain(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<LiveMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                LiveMessageMobile liveMessageMobile = (LiveMessageMobile) resultEntity.getData();
                ToastUtils.make().show("本次抢到了" + liveMessageMobile.getDiamond() + "钻石");
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 领取平台红包雨
     */
    public void getRedsrains(){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("user_id",MerchantSession.getInstance().getUser().getUser_id());
        paramsa.put("live_id",startLiveMobileObservableField.get().getLive_id());
        addSubscribe(apiCallInterface.getRedsrains(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<LiveMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                LiveMessageMobile liveMessageMobile = (LiveMessageMobile) resultEntity.getData();
                ToastUtils.make().show("本次抢到了" + liveMessageMobile.getDiamond() + "钻石");
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 主播接的任务列表
     */
    public void myTaskList(){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("status",0);
        addSubscribe(apiCallInterface.myTaskList(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<SellGoodsMobile>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<SellGoodsMobile> sellGoodsMobileList = (List<SellGoodsMobile>) resultEntity.getData();
                mSellGoodsMobileList.setValue(sellGoodsMobileList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }


    /**
     * 修改任务状态（是否带货中）
     */
    public void taskInfo(SellGoodsMobile sellGoodsMobile){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("live_id",startLiveMobileObservableField.get().getLive_id());
        paramsa.put("id",sellGoodsMobile.getId());
        paramsa.put("type",2);
        addSubscribe(apiCallInterface.taskInfo(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                //主播接的任务列表
                myTaskList();
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 主播结束任务
     */
    public void finishTask(SellGoodsMobile sellGoodsMobile){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("id",sellGoodsMobile.getId());
        addSubscribe(apiCallInterface.finishTask(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ToastUtils.make().show(resultEntity.getMsg());
                //主播接的任务列表
                myTaskList();
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }
}
